package com.guanyu.u2cache.api;

import com.guanyu.u2cache.cache.CacheOperate;
import com.guanyu.u2cache.constant.CacheConstant;
import com.guanyu.u2cache.enums.CacheType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author zhouyunjian
 * @date 2023/12/08 11:39
 * u2Cache手动调用api接口信息
 * 支持对外接口显性调用
 **/
@Service
public class U2CacheOperateApi<K, V> {

    @Autowired
    private CacheOperate cacheOperate;


    /**
     * 塞入缓存信息
     * expireTime<=0时，代表无过期时间
     *
     * @param cacheType
     * @param key
     * @param expireTime
     * @param timeUnit
     */
    public Object putCache(CacheType cacheType, String key, Object result, long expireTime, TimeUnit timeUnit) {
        cacheOperate.putCache(new String[]{CacheConstant.operateCacheName}, key, cacheType, result, null, expireTime, timeUnit);
        return result;
    }


    /**
     * todo 时间有效性逻辑待优化处理，根据原有规则处理
     *
     * @param cacheType
     * @param key
     * @return
     */
    public Object getCache(CacheType cacheType, String key) {
        return cacheOperate.getCache(new String[]{CacheConstant.operateCacheName}, key, cacheType, 0, TimeUnit.SECONDS);
    }
}
